/***************************************************************************
 *   Copyright (C) 2009 by Bernhard Neuhofer   *
 *   mail@bneu.at   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#ifndef SUBMODELMANAGER_H
#define SUBMODELMANAGER_H

using namespace std;
#include <map>
#include <string>
#include "AbstractLp.h"
#include "messageHandler.h"
#include "Scheduler.h"
#include "smOutput.h"
#include "gvtStats.h"
#include "factories.h"

/**
* @brief Creates LPs and its corresponding message handlers, adapters and model entities
* and setups the communication netzwork between LPs at simulation startup.
* Repeatedly calls the <code>doStep</code> method its LPs and works as
* intermediator for any communication between main program and its LPs.
*/
class SubmodelManager
{
private:
  
  SubmodelManager() {};
  SubmodelManager( const SubmodelManager&);
  SubmodelManager& operator = (const SubmodelManager &);  
  
/** ID of the submodel manager */
unsigned int _id;
//Hash with pairs for every LP of the submodel Manager
map<unsigned int,AbstractLp*> _lpRefs;

//Reference to the associated message handler
MessageHandler* _assocMh;

//Reference to the associated scheduler
Scheduler* _assocSched;

//Reference to the associated Output collector
SmOutput* _smOutput;

//Reference to the associated GvtStats object, used by the GVT algo.
GvtStats* _gvtStats;

//Stop simulation when endGvt is reached
double _endGvt;



public:
   static SubmodelManager& instance()
     {
	   static SubmodelManager _instance;
	   return _instance;
	 }
	 ~SubmodelManager() {};

/**
 * Constructs a submodel manager Instanciates and links Message Handler,
 * Scheduler and GvtStats objects
 * @param id 
 * @param mh 
 */
void initSubmodelManager(unsigned int id, MessageHandler* mh)
{
  _id=id;
  _assocMh=mh;
  _endGvt=-1.0;
cout<<"color output"<<endl;
	_gvtStats=new GvtStats();
	cout<<_gvtStats->getColor()<<"color output"<<endl;
	_smOutput=new SmOutput();
}

/**
 * The scheduler is instanciated
 * @param schedClass 
 * @param endGvt 
 */
void initSched(string schedClass, double endGvt);

/**
 * Initialsises a LP/ME plus adapter. The submodel manager plays the
 * the roll of a factory class for LPs, MEs and adapters here
 * @param id The ID of the logical process
 * @param neighbors IDs of the logical processes known to this one
 * @param meClass Model Entity class
 * @param lpClass Logical Process class
 * @param adClass Adapter class
 */
void initLp(unsigned int id, vector<unsigned int> neighbors, string meClass, string lpClass, string adClass);


/**
 * Main program calls this method to start the simulation in this submodel manager.
 * The method is remote-inovkeable and returns right after it spawns the thread.
 */
void startSim();


/**
 * The <code>run</code> method is called by <code>startSim</code> to spawn a SubmodelManager thread.
 * The thread repeatedly calls the <code>so Step</code> method of the local LPs according to our
 * scheduling strategy unitl some termination criterion is met.
 */
void run();

/**
 * Uses by the GVT manager to issue a cut (C or C')
 * @param CutC shows if this is an initial cur (Cut C)
 * @return GvtStats
 */
GvtStats* cut(bool CutC);

/**
 * Returns the output measures collected for this SM
 * @return 
 */
SmOutput* collectOutput();

/**
 * Used by the main program to shut the SM down.
 */
void shutdown();

/**
 * Returns the associated message handler of the submodel manager
 * @return 
 */
MessageHandler* getAssocMh();


/**
 * Sets the Gvt. Remotely invocable method which is used to push new Gvt values
 * to the Submodel Manager for fossil collection.
 * @param gvt double
 */
void setGvt(double gvt);

};

#endif
